//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   ACCESS_TEAM
// AUTHOR           :   Jerry Chen 
// DATE CREATE      :   2007/10/26
// DESCRIPTION      :   a COMMON DAO file for the function, ACCESS_TEAM
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/10/26   Jerry Chen      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.accessTeamMaster.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.setup.accessTeamMaster.dao.AccessTeamCommonDao;
import com.erp.app.setup.accessTeamMaster.vo.AccessTeamHdrVo;
import com.erp.app.setup.accessTeamMaster.bizLogic.AccessTeamCommonUtil;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.types.CustomSqlDate;
import com.erp.framework.util.CommonUtil;
import com.erp.app.setup.accessTeamMaster.criteria.AccessTeamHdrSrchCriteria;

/**
* <p>A <strong>CommonDao</strong> provides database access functions
*  for delegate classes.
*
* @author          Jerry Chen 
* @version 1.0     2007/10/26
* @since           FrameWork 1.1
*
*/
public class AccessTeamCommonDao {

	private AccessTeamCommonUtil util = new AccessTeamCommonUtil();

	/**
	* Returns the SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing sql query
	*
	* @author     Jerry Chen 
	* @since      Framework 1.1
	*/
	public static String getHdrSqlQuery(SimpleBaseSrchCriteria obj) {

		AccessTeamHdrSrchCriteria criteria = (AccessTeamHdrSrchCriteria) obj;
		String sql = "";
		String whereClause = hdrToWhereClause(criteria);
		String groupByClause = "";
		String orderByClause = "ORDER BY COMPANY_CODE, ACCESS_TEAM_CODE";

		if(!criteria.isSearchResult()){
			sql = "SELECT COMPANY_CODE, ACCESS_TEAM_CODE, ACCESS_TEAM_NAME, DESCRIPTION, STATUS, "
			+ "DC_IND, DC_DATE, CREATE_USER, CREATE_DATE, UPDATE_USER, "
			+ "UPDATE_DATE "
			+ "FROM T_ACCESS_TEAM_HDR "
			+ whereClause
			+ groupByClause
			+ orderByClause;
		} else {
			sql = "SELECT COMPANY_CODE, ACCESS_TEAM_CODE, ACCESS_TEAM_NAME, DESCRIPTION, STATUS, "
				+ "DC_IND, DC_DATE, CREATE_USER, CREATE_DATE, UPDATE_USER, "
				+ "UPDATE_DATE "
				+ "FROM T_ACCESS_TEAM_HDR "
				+ whereClause
				+ groupByClause
				+ orderByClause;
		}

		return sql;
	}

	/**
	* Sets the values of a result set to a vo for a search screen
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Jerry Chen 
	* @since      Framework 1.1
	*/
	public static SimpleBaseVo getHdrSearchResultSetValues(ResultSet rs) throws SQLException {

		AccessTeamHdrVo vo = new AccessTeamHdrVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setAccessTeamCode(rs.getString("ACCESS_TEAM_CODE"));
		vo.setHdrAccessTeamName(rs.getString("ACCESS_TEAM_NAME"));
		vo.setHdrDescription(rs.getString("DESCRIPTION"));
		vo.setHdrStatus(rs.getString("STATUS"));
		vo.setHdrDcInd(rs.getString("DC_IND"));
		vo.setHdrDcDate(CustomSqlDate.customize(rs.getDate("DC_DATE")));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));

		return vo;

	}

	/**
	* Returns the where clause of a SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing the where clause
	*
	* @author     Jerry Chen 
	* @since      Framework 1.1
	*/
	public static String hdrToWhereClause(AccessTeamHdrSrchCriteria crit) {

		AccessTeamHdrSrchCriteria criteria = (AccessTeamHdrSrchCriteria) crit;
		StringBuffer whereClause = new StringBuffer();

		if (criteria.getSrchCompanyCode() != null) {
			whereClause.append(whereClause.length()== 0? "WHERE ": "AND ");
			whereClause.append(" COMPANY_CODE = '" + criteria.getSrchCompanyCode().trim().toUpperCase()+"' ");
		}
		CommonUtil.addHdrBetween(whereClause, "ACCESS_TEAM_CODE", criteria.getSrchAccessTeamCodeFr(), criteria.getSrchAccessTeamCodeTo());   
        CommonUtil.addHdrLike(whereClause, "ACCESS_TEAM_NAME", criteria.getSrchHdrAccessTeamNameFr());
        CommonUtil.addHdrEquals(whereClause, "STATUS", criteria.getSrchHdrStatus());

		return whereClause.toString();
	}

}
